/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.LogFile;
import pojo.Scheduledata;

/**
 *
 * @author javauser
 */
//this class for all operations related to schedule task in database...
public class ScheduleDataDao {

    Connection con;

    public ScheduleDataDao(Connection con) {
        this.con = con;
    }

    //this method for adding new schedule data in database...
    public boolean add(Scheduledata schdata) {
        boolean check = false;
        try {
            PreparedStatement ps = con.prepareStatement("insert into scheduledata values( ? , ? , ? , ? , ? , ? , ? , ? , ?)");
            ps.setInt(1, schdata.getScId());
            ps.setString(2, schdata.getSchedule());
            ps.setString(3, schdata.getModifier());
            ps.setDate(4, schdata.getStartDate());
            ps.setString(5, schdata.getStartTime());
            ps.setString(6, schdata.getDay());
            ps.setBoolean(7, schdata.getIsenable());
            ps.setBoolean(8, schdata.getIsrunning());
            ps.setString(9, schdata.getMonth());
            int i = ps.executeUpdate();
            if (i == 1) {
                check = true;
                LogFile.log(" System ", " successfully added schedule task with ID :[" + schdata.getScId() + "] to scheduledata table ");
            } else {
                check = false;
                LogFile.log(" System ", " failed to add schedule task with ID :[" + schdata.getScId() + "] to scheduledata table .");
            }
        } catch (SQLException ex1) {
            ex1.printStackTrace();
            LogFile.log(" System ", " encountered the following exception while trying to add schedule task with ID :[" + schdata.getScId() + "] ,and this is the Problem : [" + ex1 + "]");
        }
        return check;
    }

    //this method for deleting schedule task from database
    public boolean delete(int id) {
        boolean check = false;
        try {
            PreparedStatement pstmt = con.prepareStatement("delete from scheduledata where sc_id=?");
            pstmt.setInt(1, id);
            int i = pstmt.executeUpdate();
            if (i == 1) {
                check = true;
            } else {
                check = false;
            }
        } catch (SQLException ex1) {
            ex1.printStackTrace();
            LogFile.log(" System ", " faces the following exception during the deleting operaion :  " + ex1.getMessage());
        }
        return check;
    }

    //this method updates schedule task data...
    public boolean update(Scheduledata schdata) {
        boolean check = false;
        try {
            boolean first = false;
            String updatetxt = "";
            if (schdata.getSchedule() != null) {
                first = true;
                updatetxt = "schedule='" + schdata.getSchedule() + "'";
            }
            if (schdata.getModifier() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "modifier='" + schdata.getModifier() + "'";
            }
            if (schdata.getStartDate() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "start_date='" + schdata.getStartDate() + "'";
            }
            if (schdata.getStartTime() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "start_time='" + schdata.getStartTime() + "'";
            }
            if (schdata.getDay() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "day='" + schdata.getDay() + "'";
            }
            if (schdata.getMonth() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "month='" + schdata.getMonth() + "'";
            }
            if (first) {
                updatetxt += " , ";
            }
            updatetxt += " isenable = " + schdata.getIsenable() + " , isrunning = " + schdata.getIsrunning() + " ";
            PreparedStatement ps = con.prepareStatement("update scheduledata set " + updatetxt + " where sc_id=?");
            ps.setInt(1, schdata.getScId());
            int i = ps.executeUpdate();
            ProviderDao pdao = new ProviderDao(con);
            if (i == 1) {
                check = true;
                LogFile.log(" System ", " Successffully Updated of Schedule Task related to Provider :[" + pdao.getById(schdata.getScId()).getName() + "]");
            } else {
                check = false;
                LogFile.log(" System ", " failed to update Task Data  of Provider +[" + pdao.getById(schdata.getScId()).getName() + "]");
            }
        } catch (SQLException ex) {
            check = false;
            ex.printStackTrace();
            LogFile.log(" System ", "encountered the Following exception during the update operation of the Task with ID [" + schdata.getScId() + "]and this is the message : " + ex);
        }
        return check;
    }

    public Vector<Scheduledata> getall() {
        Vector<Scheduledata> tasks = new Vector<Scheduledata>();
        try {

            PreparedStatement ps = con.prepareStatement("select * from scheduledata");
            ResultSet result = ps.executeQuery();
            String str = "";
            while (result.next()) {
                Scheduledata sc = new Scheduledata();
                System.out.println("sccccccccccccccccccc" + result.getString(2));
                sc.setScId(result.getInt(1));
                sc.setSchedule(result.getString(2));
                sc.setModifier(result.getString(3));
                if (result.getString(4) != null) {
                    sc.setStartDate(result.getDate(4));
                }
                if (result.getString(5) != null) {
                    sc.setStartTime(result.getString(5));
                }
                if (result.getString(6) != null) {
                    sc.setDay(result.getString(6));
                }
                if (result.getString(7) != null) {
                    sc.setIsenable(result.getBoolean(7));
                }
                if (result.getString(8) != null) {
                    sc.setIsrunning(result.getBoolean(8));
                }
                if (result.getString(9) != null) {
                    sc.setMonth(result.getString(9));
                }
                tasks.add(sc);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ScheduleDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tasks;

    }

    public Scheduledata getask(int id) {
        try {
            PreparedStatement ps = con.prepareStatement("select * from scheduledata where sc_id=? ");
            ps.setInt(1, id);
            ResultSet result = ps.executeQuery();
            String str = "";
            result.next();
            Scheduledata sc = new Scheduledata();
            sc.setScId(result.getInt(1));
            sc.setSchedule(result.getString(2));
            sc.setModifier(result.getString(3));
            sc.setStartDate(result.getDate(4));
            sc.setStartTime(result.getString(5));
            sc.setDay(result.getString(6));
            sc.setIsenable(result.getBoolean(7));
            sc.setIsrunning(result.getBoolean(8));
            sc.setMonth(result.getString(9));
            return sc;
        } catch (SQLException ex) {
            Logger.getLogger(ScheduleDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
